package com.coocaa.ops.admin.v1.panel.validator;

import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.plus.core.third.magic.model.AiIconInfoModel;
import com.coocaa.ops.admin.v1.panel.constant.AiIconConstants;
import com.coocaa.ops.common.core.biz.panel.constant.BlockTypeConstants;
import com.coocaa.ops.common.core.biz.panel.entity.BlockContentConfigInfo;
import com.coocaa.ops.common.tool.json.JsonUtil;
import com.coocaa.ops.common.tool.string.StringUtil;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/19 11:49
 */
@UtilityClass
public class BlockValidator {

    public static void validIconText(String iconTextPositionsJsonStr){
        if (StringUtils.isBlank(iconTextPositionsJsonStr)){
            return;
        }
        List<AiIconInfoModel> iconTextPositions = JsonUtil.json2ObjectLists(iconTextPositionsJsonStr, AiIconInfoModel.class);
        for (AiIconInfoModel aiIconInfoModel : iconTextPositions){
            valid(aiIconInfoModel);
        }
    }


    public static void  valid(BlockContentConfigInfo blockContentConfigInfo, boolean isDmp){
        Assert.hasText(blockContentConfigInfo.getBlockType(), "推荐位类型不能为空");
        if (!BlockTypeConstants.PLUGIN.equals(blockContentConfigInfo.getBlockType())) {
            Assert.hasText(blockContentConfigInfo.getCoverType(), "coverType不能为空");
            Assert.hasText(blockContentConfigInfo.getVideoContentType(), "videoContentType不能为空");
            Assert.hasText(blockContentConfigInfo.getPictureUrl(), "海报不能为空,pictureUrl");
        }
        if(isDmp){
            InputParamsValidator.validDmpContentAvailable(blockContentConfigInfo.getDmpRegistryInfo());
        }
    }

    private static void valid(AiIconInfoModel aiIconInfoModel){
        Assert.notNull(aiIconInfoModel,"智能角标信息不能为空");
        Assert.hasText(aiIconInfoModel.getIconText(),"智能角标文字不能为空");
        Assert.hasText(aiIconInfoModel.getPosition(),"智能角标位置不能为空");
        int length = aiIconInfoModel.getIconText().length();
        int chineseWordCount = StringUtil.countChineseWord(aiIconInfoModel.getIconText());
        int total = chineseWordCount * 2 + (length - chineseWordCount);
        Assert.isTrue(total <= AiIconConstants.TOTAL_WORD_NUMS, "角标文字长度超过限定字符: " + aiIconInfoModel.getIconText());
        Assert.isTrue(AiIconConstants.ICON_POSITIONS.contains(aiIconInfoModel.getPosition()),"角标位置不符合,should be one of "+JsonUtil.obj2Json(AiIconConstants.ICON_POSITIONS));

    }
}
